package com.example.studentstatusmanagement.controller;

import com.example.studentstatusmanagement.config.SystemVariable;
import com.example.studentstatusmanagement.pojo.Major;
import com.example.studentstatusmanagement.pojo.MajorExample;
import com.example.studentstatusmanagement.pojo.doo.ReqMajor;
import com.example.studentstatusmanagement.pojo.doo.ReqStudent;
import com.example.studentstatusmanagement.pojo.dto.SMajor;
import com.example.studentstatusmanagement.pojo.dto.Student;
import com.example.studentstatusmanagement.service.MajorService;
import com.example.studentstatusmanagement.utils.ResultResponse;
import com.example.studentstatusmanagement.utils.ResultTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/v1/major")
public class MajorController {

    @Autowired
    private MajorService majorService;

    @RequestMapping("show")
    public Map<String, Object> showMajorsInfo(ReqMajor reqMajor) {

        Map<String, Object> so = new HashMap<String, Object>();

        List<SMajor> ls = majorService.selectMajorsInfo(reqMajor);

        so.put("code", SystemVariable.URL_SUCCESS);
        so.put("msg", "");
        so.put("count", ls.size());
        so.put("data", ls);

        return so;
    }

    @RequestMapping("del")
    public ResultResponse del(Major major){
        if (!StringUtils.isEmpty(major.getId())) {
            major.setIsdel(SystemVariable.IS_DEL);
            if (majorService.updateByPrimaryKeySelective(major) == SystemVariable.SUCCESS) {
                return ResultResponse.Build(ResultTypeEnum.success, SystemVariable.DO_SUCCESS);
            }
        }
        return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.DO_ERROR);
    }
    @RequestMapping("use")
    public ResultResponse use(Major major){
        if (!StringUtils.isEmpty(major.getId())) {
            major.setIsdel(SystemVariable.IS_NOT_DEL);
            if (majorService.updateByPrimaryKeySelective(major) == SystemVariable.SUCCESS) {
                return ResultResponse.Build(ResultTypeEnum.success, SystemVariable.DO_SUCCESS);
            }
        }
        return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.DO_ERROR);
    }
    @RequestMapping("update")
    public ResultResponse update(Major major){
        major.setCollege("电子与信息工程学院");
        MajorExample example = new MajorExample();
        MajorExample.Criteria c = example.createCriteria();
        c.andMajorNameEqualTo(major.getMajorName());
        c.andIdNotEqualTo(major.getId());
        if (majorService.selectByExample(example).size()>0)
            return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.IS_HAVE);
        if (!StringUtils.isEmpty(major.getId())) {
            if (majorService.updateByPrimaryKeySelective(major) == SystemVariable.SUCCESS) {
                return ResultResponse.Build(ResultTypeEnum.success, SystemVariable.DO_SUCCESS);
            }
        }
        return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.DO_ERROR);
    }

    @RequestMapping("add")
    public ResultResponse add(Major major){
        major.setCollege("电子与信息工程学院");
        MajorExample example = new MajorExample();
        MajorExample.Criteria c = example.createCriteria();
        c.andMajorNameEqualTo(major.getMajorName());
        if (majorService.selectByExample(example).size()>0)
            return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.IS_HAVE);
        if (majorService.insertSelective(major) == SystemVariable.SUCCESS) {
                return ResultResponse.Build(ResultTypeEnum.success, SystemVariable.DO_SUCCESS);
        }
        return ResultResponse.Build(ResultTypeEnum.error, SystemVariable.DO_ERROR);
    }
}
